/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-09 11:04:01
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-18 20:54:13
 * @FilePath: \实训一git\app\src\router\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createRouter, createWebHistory } from 'vue-router'

import store from '../store'
const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path:'/login',
    name:'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue'),
    meta: {
      title: '登录',
    },
  },
  {
    path: '/sign',
    name: 'sign',
    component: () => import(/* webpackChunkName: "about" */ '../views/Sign.vue'),
    meta: {
      title: '注册',
    },
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})


const whitlist = ['/login','/sign'];

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('token');
  console.log(to,'ksdsdsdsd')
  document.title=to.meta.title
  if (whitlist.includes(to.path)) {
    next();
  } else {

    if (token) {
      let routerList = store.state.login.routerList;
      let author = localStorage.getItem('author');
      console.log(store.state,'kkk')
      if (routerList.length <= 0 && author) {
        store.dispatch('login/setRouterList', author);
        next({ ...to, replace: true });
      } else {
        next();
      }

    } else {
      next('/login');
    }

  }
})

export default router
